const app = getApp();
Page({
  data: {
    key: '',
    employeeId: '',
    employeeName: '',
    item: '',
    items: '',
    suggestion: '',
    type: 1,
    objectArray: [
      {
        id: '1',
        name: '同意'
      },
      {
        id: '0',
        name: '拒绝'
      }
    ],
    arrIndex: 0,
    display_spyj: 'none',
    btnText: ''
  },
  onLoad: function(option) {
    if (option.single == 'true') {
      // for (var i = 0; i < app.globalData.item.details.length; i++) {
      //   delete app.globalData.item.details[i].array;
      //   delete app.globalData.item.details[i].value;
      // }
      this.setData({
        item: JSON.stringify(app.globalData.item),
        type: option.type,
        display_spyj: 'none'
      })
    }
    else {
      // for (var i = 0; i < app.globalData.items.length; i++) {
      //   for (var j = 0; j < app.globalData.items[i].details.length; j++) {
      //     delete app.globalData.items[i].details[j].array;
      //     delete app.globalData.items[i].details[j].value;
      //   }
      // }
      this.setData({
        items: JSON.stringify(app.globalData.items),
        type: option.type,
        display_spyj: 'block'
      })
    }
    this.setData({
      key: app.globalData.key,
      employeeId: app.globalData.employeeId,
      employeeName: app.globalData.employeeName
    })
    if (option.type == 1) {
      this.setData({
        btnText: '提交[同意]',
      })
    }
    else {
      this.setData({
        btnText: '提交[拒绝]',
      })
    }
  },
  onSuggestionInput(e) {
    this.setData({
      suggestion: e.detail.value
    })
  },
  bindObjPickerChange(e) {
    this.setData({
      arrIndex: e.detail.value,
      type: this.data.objectArray[e.detail.value].id,
    });
    if (this.data.objectArray[e.detail.value].id == 1) {
      this.setData({
        btnText: '提交[同意]',
      })
    }
    else {
      this.setData({
        btnText: '提交[拒绝]',
      })
    }
  },
  onSubmit() {
    dd.showLoading({
      content: '加载中...',
      delay: 1000,
    });
    if (this.data.item != '') {
      var item = JSON.parse(this.data.item);
      for (var i = 0; i < item.details.length; i++) {
        delete item.details[i].array;
        delete item.details[i].value;
      }
      this.setData({
        item: JSON.stringify(item)
      })
    }
    if (this.data.items != '') {
      var items = JSON.parse(this.data.items);
      for (var i = 0; i < items.length; i++) {
        for (var j = 0; j < items[i].details.length; j++) {
          delete items[i].details[j].array;
          delete items[i].details[j].value;
        }
      }
      this.setData({
        items: JSON.stringify(items)
      })
    }
    //console.log(this.data.item);
    //return;
    dd.httpRequest({
      url: app.globalData.doMainName + '/dingapi/workflow/approval2.ashx',
      method: 'POST',
      data: this.data,
      dataType: 'text',
      async: false,
      success: function(res) {
        dd.hideLoading();
        console.log(res.data);
        if (res.data == '999') {
          dd.alert({ content: '工号错误' });
        }
        else if (res.data == '-1') {
          dd.alert({ content: '提交失败' });
        }
        else if (res.data == '0') {
          dd.alert({ content: '部分提交成功' });
        }
        else if (res.data == '1') {
          //dd.alert({ content: '提交成功' });
          dd.reLaunch({
            url: '../../pages/main/main',
            success: function(res) {
            },
            fail: function(err) {
            },
            complete: function(res) {
            }
          });
        }
        else {
          dd.alert({ content: res.data });
        }
      },
      fail: function(err) {
        dd.alert({ title: JSON.stringify(err) });
        console.log(JSON.stringify(err));
      }
    });
  },
});
